"""
Case Type   : USTORE
Case Name   : 验证参数enable_ustore的生效机制
Create At   : 2025/3/17
Owner       : wan005
Description :
    1.查询参数默认值
    2.修改enable_ustore值为off
    3.创建ustore表
    4.查看表storage_type
Expect      :
    1.参数默认值off
    2.修改成功
    3.创建失败
History     :
"""
import os
import unittest
import sys
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table = 't_ustore_01'

    def test_ustore(self):
        text = '-----step1:查询参数默认值;expect:参数默认值on-----'
        self.logger.info(text)
        res1 = self.common.show_param('enable_ustore')
        self.assertEqual('on', res1, text + '执行失败')

        text = '----step2:修改enable_ustore值为off;expect:成功----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('set',
            self.constant.GSGUC_SUCCESS_MSG, 'enable_ustore=off')
        restart_msg = self.sh_primy.restart_db_cluster()
        self.logger.info(restart_msg)
        res2 = self.common.show_param('enable_ustore')
        self.assertEqual('off', res2, text + '执行失败')

        text = '-----step3:创建表;expect:创建失败'
        sql3 = f'drop table if exists {self.table};' \
               f'create table {self.table}(age int primary key) ' \
                   f'with (storage_type=ustore);'
        res3 = self.sh_primy.execut_db_sql(sql3)
        self.logger.info(res3)
        self.assertIn('Ustore is disabled, please set enable_ustore=on',
                      res3, text + '执行失败')

    def tearDown(self):
        text = '-----step1:清理环境，恢复配置;expect:成功-----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                    'enable_ustore=on')
        restart_msg = self.sh_primy.restart_db_cluster()
        self.logger.info(restart_msg)
        res1 = self.common.show_param('enable_ustore')
        self.assertEqual('on', res1, text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
